<script setup lang="ts">
import { routes } from "../router/routes.ts";
import { ref } from "vue";
import { useRoute, useRouter } from "vue-router";
const router = useRouter();
const route = useRoute();

// 路由跳转时，更新选中的菜单项
router.afterEach((to, from, failure) => {
  selectedKeys.value = [to.path];
});

// 默认主页
const selectedKeys = ref([route.path]);
const doMenuClick = (key: string) => {
  router.push({
    path: key,
  });
};
</script>
<template>
  <div class="globalHeader">
    <a-menu
      mode="horizontal"
      :default-selected-keys="[selectedKeys]"
      @menu-item-click="doMenuClick"
    >
      <a-menu-item
        key="0"
        :style="{ padding: 0, marginRight: '38px' }"
        disabled
      >
        <div class="title-bar">
          <img class="logo" src="../assets/oj-logo.png" />
          <div class="title">MyOJ</div>
        </div>
      </a-menu-item>
      <a-menu-item v-for="item in routes" :key="item.path">
        {{ item.name }}
      </a-menu-item>
    </a-menu>
  </div>
</template>
<style scoped>
.menu-demo {
  box-sizing: border-box;
  width: 100%;
  padding: 40px;
  background-color: var(--color-neutral-2);
}
.title-bar {
  display: flex;
  align-items: center;
}
.title {
  color: #444;
}
.logo {
  height: 40px;
}
</style>
